<template>
  <div>
    <el-form :model="tageQuery" inline>
      <el-form-item label="标签名称">
        <el-input v-model="tageQuery.tagName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getShareTagPage">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button  icon="Plus" @click="dialogVisible=true">添加</el-button>
    <el-table :data="tagPage.list" border stripe v-loading="loading">
      <el-table-column type="index"></el-table-column>
      <el-table-column label="标签名称" prop="name"></el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button icon="Edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button icon="Delete" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
        v-model="dialogVisible"
        title="标签"
        width="500"
        @close="handleClose"
    >
      <el-form :model="shareTagForm" ref="tageFormRef" label-width="80px">
        <el-form-item label="标签名称">
          <el-input v-model="shareTagForm.name" placeholder="请输入标签名称"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="shareTagForm.description" placeholder="请输入描述"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitSaveTag(tageFormRef)">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive, onMounted, watch} from "vue";
import {reqCreateTag, reqDeleteTag, reqGetTagPage} from "@/api/share/tag";
import type {FormInstance} from 'element-plus'
import {ElMessage, ElMessageBox} from 'element-plus'

let loading = ref(true)
let tageFormRef = ref()
let isEditing = ref(false)
let dialogVisible = ref(false)
let tageQuery = reactive({
  categoryId: '',
  tagName: '',
  pageNum: 1,
  pageSize: 10
})
let tagPage = reactive({
  total: 0,
  list: [],
  pageNum: 1,
})
let shareTagForm = reactive({
  tagId: null,
  name: '',
  description: '',
})

let getShareTagPage = async () => {
  loading.value = true
  let res = await reqGetTagPage(tageQuery)
  Object.assign(tagPage, res.data)
  loading.value = false
}
let handleClose = () => {
  shareTagForm.tagId = null
  shareTagForm.name = ''
  shareTagForm.description = ''
}
let submitSaveTag = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await reqCreateTag(shareTagForm)
    } else {
      console.log('error submit!', fields)
    }
  })
  await getShareTagPage()
  dialogVisible.value = false
}
let handleEdit = (node) => {
  isEditing.value = true
  Object.assign(shareTagForm, node)
  dialogVisible.value = true
}
let handleDelete = async (row) => {
  ElMessageBox.confirm(
      `删除之后无法恢复，请问是否删除${row.name}?`,
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      } as string
  )
      .then(async () => {
        await reqDeleteTag(row.tagId)
        getShareTagPage()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}
onMounted(() => {
  getShareTagPage()
})
watch([() => tageQuery.pageNum, () => tageQuery.pageSize], () => {
  getShareTagPage()
})
</script>

<style scoped>

</style>
